package infopreserve.main.mobile;

import infopreserve.main.mobile.R;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Search extends Activity{
	protected String entered;
	protected ArrayList<String> searched;
	protected AllFiles f;
	protected String[] newSearch;
	protected Dialog dialog;
	protected InputMethodManager mgr;
	protected EditText editText;
	protected TextView noItems;
	protected ListView lv;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		setContentView(R.layout.search);
		editText = (EditText)findViewById(R.id.searchText);
		editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		noItems = (TextView)findViewById(R.id.noitems);
		mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
		editText.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
		        if (keyCode == KeyEvent.KEYCODE_ENTER) {
		            Click(editText);
		            return true;
		        }
		        return false;
		    }
		});
	}
	public void Click(View view){
		editText.selectAll();
		noItems.setVisibility(4);
		if(lv != null)
			lv.setVisibility(0);
		f = new AllFiles();
		searched = new ArrayList<String>();
		entered = ((EditText)findViewById(R.id.searchText)).getText().toString();
		mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		for(int i = 0; i < f.files.length-1; i++){
			if(entered.equals(f.files[i])){
				searched.add(f.files[i]);
			}
		}
		if(searched.size() == 0){
			noItems.setVisibility(0);
		}
		newSearch = new String[searched.size()];
		newSearch = searched.toArray(newSearch);
		newListView(newSearch);
	}
	public void newListView(String[] items){
		lv = (ListView)findViewById(R.id.list);
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.file, items));
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				f.filename = ((TextView)view).getText().toString();
				onButtonPopup(view);
			}
		});
	}
	
    public void onButtonPopup (final View target) {
    	dialog = new Dialog(Search.this);
    	dialog.setContentView(R.layout.detail);
    	dialog.setTitle(f.filename);
    	final Button button = (Button) dialog.findViewById(R.id.button1);
    	Button b = (Button) dialog.findViewById(R.id.close);
    	dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    	dialog.show();

    	button.setOnClickListener(new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			Toast.makeText(target.getContext(), "Starting Download...", Toast.LENGTH_LONG).show();
    		}
    	});

    	b.setOnClickListener(new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			dialog.dismiss();

    		}
    	});

    }
	public void onPause(){
		super.onPause();
		mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		noItems.setVisibility(4);
		if(lv != null)
			lv.setVisibility(4);
		editText.setText("");
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.settings:
                //method here
                return true;
            case R.id.info:
	 		     Intent intent = new Intent();
	 		     intent.setClass(this,Info.class);
	 		     startActivity(intent);
                return true;
            case R.id.signout:
	 		     Intent i = new Intent();
	 		     i.setClass(this,InfoPreserveActivity.class);
	 		     startActivity(i);
	 		     this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

	public void onBackPressed(){
		return;
	}
}
